//! 直接WebSocket连接测试
//! 
//! 使用tokio-tungstenite直接测试WebSocket连接

use tokio_tungstenite::{connect_async, tungstenite::Message};
use url::Url;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 初始化日志
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();

    println!("🔧 直接WebSocket连接测试");
    println!("==========================");

    // 测试不同的WebSocket URL
    let test_urls = vec![
        "wss://demo-ws-api.binance.com",
        "wss://stream.binance.com:9443",
        "wss://fstream.binance.com",
        "wss://dstream.binance.com",
    ];

    for url_str in test_urls {
        println!("\n🔌 测试连接: {}", url_str);
        
        match Url::parse(url_str) {
            Ok(url) => {
                println!("   ✅ URL解析成功: {}", url);
                
                match connect_async(url).await {
                    Ok((ws_stream, _)) => {
                        println!("   ✅ WebSocket连接成功！");
                        
                        // 等待一段时间
                        sleep(Duration::from_secs(1)).await;
                        
                        // 关闭连接
                        drop(ws_stream);
                        println!("   🔌 连接已关闭");
                    },
                    Err(e) => {
                        println!("   ❌ WebSocket连接失败: {}", e);
                    }
                }
            },
            Err(e) => {
                println!("   ❌ URL解析失败: {}", e);
            }
        }
        
        // 等待一段时间再测试下一个
        sleep(Duration::from_secs(2)).await;
    }

    println!("\n🎉 所有WebSocket连接测试完成！");
    Ok(())
}
